package com.agrm.address.service.impl;

import com.agrm.address.mapper.ChinaRegionsMapper;
import com.agrm.address.service.ChinaRegionsService;
import com.agrm.model.address.pojo.ChinaRegions;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChinaRegionsServiceImpl implements ChinaRegionsService {
    private ChinaRegionsMapper chinaRegionsMapper;

    @Autowired
    public void setChinaRegionsMapper(ChinaRegionsMapper chinaRegionsMapper) {
        this.chinaRegionsMapper = chinaRegionsMapper;
    }

    @Override
    public List<ChinaRegions> findAll(ChinaRegions chinaRegions) {
        return chinaRegionsMapper.findAll(chinaRegions);
    }

    @Override
    public List<ChinaRegions> findAll() {
        return chinaRegionsMapper.selectList(null);
    }

    //树型结构
    @Override
    public List<ChinaRegions> findTree() {
        // 直接调用方法，而不是通过代理
        List<ChinaRegions> categories = this.findAll();
        return makeTree(categories);
    }

    //树型结构
//    @Override
//    public ChinaRegions findById(Integer id) {
//        // 直接调用方法，而不是通过代理
//        List<ChinaRegions> roots = this.findTree();
//        return findById(roots, id);
//    }

    @Override
    public ChinaRegions findById(Integer id) {
        if (id == null || id <= 0) {
            return null;
        }

        // 直接根据ID查询单个地区信息
        ChinaRegions region = this.chinaRegionsMapper.selectById(id);
        if (region != null && region.getParentId() != null) {
            // 加载父级信息
            ChinaRegions parent = this.chinaRegionsMapper.selectById(region.getParentId());
            region.setParent(parent);
        }

        return region;
    }




    @Override
    public List<ChinaRegions> findSubTreeById(Integer id) {
        if (id == null || id <= 0) {
            return new ArrayList<>();
        }

        // 先确认该节点存在
        ChinaRegions root = this.chinaRegionsMapper.selectById(id);
        if (root == null) {
            return new ArrayList<>();
        }

        // 获取所有数据并构建完整树
        List<ChinaRegions> allRegions = this.findAll();
        List<ChinaRegions> tree = makeTree(allRegions);

        // 在完整树中查找指定节点及其子树
        return findSubTree(tree, id);
    }

    private List<ChinaRegions> findSubTree(List<ChinaRegions> tree, Integer id) {
        for (ChinaRegions node : tree) {
            if (node.getId().equals(id)) {
                List<ChinaRegions> result = new ArrayList<>();
                result.add(node);
                return result;
            }

            if (node.getChildren() != null && !node.getChildren().isEmpty()) {
                List<ChinaRegions> found = findSubTree(node.getChildren(), id);
                if (!found.isEmpty()) {
                    return found;
                }
            }
        }
        return new ArrayList<>();
    }










    @Override
    public boolean save(ChinaRegions chinaRegions) {
        return this.chinaRegionsMapper.insert(chinaRegions) == 1;
    }


    @Override
    public boolean update(ChinaRegions chinaRegions) {
        return this.chinaRegionsMapper.updateById(chinaRegions) == 1;
    }

    @Override
    public int deleteById(Integer id) {
        // 直接调用方法，而不是通过代理
        ChinaRegions cat = this.findById(id); // 树型结构
        if (cat.getChildren() != null) {
            throw new UnsupportedOperationException("当前分类尚有子分类，无法删除");
        }
        return this.chinaRegionsMapper.deleteById(id); // 物理删除
    }

    //生成树型结构
    private List<ChinaRegions> makeTree(List<ChinaRegions> categories) {
        Map<Integer, ChinaRegions> cache = new HashMap<>();
        for (ChinaRegions chinaRegions : categories) {
            cache.put(chinaRegions.getId(), chinaRegions);
        }

        List<ChinaRegions> roots = new ArrayList<>();
        //遍历所有分类
        for (ChinaRegions chinaRegions : cache.values()) {
            if (chinaRegions.getParentId() == null) {
                roots.add(chinaRegions);
            } else {
                ChinaRegions parent = cache.get(chinaRegions.getParentId());
                if (parent == null) {
                    throw new RuntimeException("找不到指定编号的分类");
                }
                //设置关联
                chinaRegions.setParent(parent);
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(chinaRegions);
            }
        }

        return roots;
    }

    //在一个分类集合中找指定编号的分类
    private ChinaRegions findById(List<ChinaRegions> categories, Integer id) {
        for (ChinaRegions cat : categories) {
            if (cat.getId().equals(id)) {
                return cat;
            }
            if (cat.getChildren() != null) {
                ChinaRegions found = findById(cat.getChildren(), id);
                if (found != null) {
                    return found;
                }
            }
        }
        return null;
    }
}
